var prefix = jsctx + "/battery";

var batteryTotalData = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            getSumData();
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 180;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageByBatteryTotalDataRequestVo",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10, 20, 30, 50, 100],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    showColumns: true,
                    cache: false,
                    // rightFixedColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            // sort: $("#sort").val().trim(),
                            orderNum: $("#orderNum").val().trim(),
                            carNum: $("#carNum").val().trim(),
                            carVin: $("#carVin").val().trim(),
                            bizType: $("#carType").val().trim(),
                            isError: $("#isError").val().trim(),
                            carType: $("#carType").val().trim(),
                            reportStartTime: $("#beginTime").val().trim(),
                            reportEndTime: $("#endTime").val().trim(),
                        };
                    },

                    columns: [
                        {
                            title: '序号', field: '', width: '50', align: 'center',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'orderNum', title: '订单号', align: 'center', width: '100'},
                        {field: 'outOrderNum', title: '外部订单号', align: 'center', width: '100'},
                        {field: 'carNum', title: '车牌号', width: '50', align: 'center'},
                        {field: 'carVin', title: 'vin码', width: '80', align: 'center'},
                        {field: 'carType', title: '车牌型号', width: '50', align: 'center'},
                        {field: 'reportTime', title: '报告生成时间', width: '100', class: 'operateBefore-td', align: 'center'},
                        {field: 'overallPerformance', title: '整体表现得分', width: '50', class: 'operateBefore-td', align: 'center',
                                formatter:function (value,row,index) {
                                    return Math.round(value);
                                }

                        },
                        {
                            field: 'isError',
                            title: '是否存在问题',
                            width: '50',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if (row.isError == 0) {
                                    return '否';
                                }
                                return "是";
                            }
                        },

                        {
                            title: '操作',
                            field: 'roleId',
                            width: '50',
                            align: 'center',
                            formatter: function (value, row, index) {

                                // var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="查看报告"' +
                                //     ' onclick="batteryTotalData.detailBattery(\''
                                //     + row.outOrderNum
                                //     + '\')">查看报告</a> ';
                                var address = window.location.host;
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="http://'+address+'/templates/battery/getSimpleReport.jsp?outOrderNum='+row.outOrderNum+'" title="查看报告">查看报告</a> ';
                                return a;

                            }
                        }
                    ]
                });
    }

    function getSumData() {
        $.ajax({
            url: prefix + "/getSumData",
            type: "GET",
            success: function (data) {
                console.log(data)
                if (data.status == 200) {
                    if (data.data.orderCount != null && data.data.orderCount != '') {
                        document.getElementById("orderCount").innerHTML = data.data.orderCount;
                    } else {
                        document.getElementById("orderCount").innerHTML = '0';
                    }
                    if (data.data.averagePoint != null && data.data.averagePoint != '') {
                        document.getElementById("averagePoint").innerHTML = data.data.averagePoint;
                    } else {
                        document.getElementById("averagePoint").innerHTML = '-';
                    }
                    if (data.data.errorCount != null && data.data.errorCount != '') {
                        document.getElementById("errorCount").innerHTML = data.data.errorCount;
                    } else {
                        document.getElementById("errorCount").innerHTML = '0';
                    }

                } else {
                    layer.msg(data.message);
                }
            }
        });
    }

    function reLoad() {
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#exampleTable').bootstrapTable('refresh');
        getSumData();
        // getDate()
    }


    function goBack() {
        window.history.back();
    }

    function detailBattery(outOrderNum) {
        // var beginTime=$("#beginTime").val().trim();
        // console.log("beginTime"+beginTime);
        // var endTime=$("#endTime").val().trim();
        // console.log("endTime"+endTime);
        // location.href = prefix + "/getSimpleReport?outOrderNum=" + outOrderNum;
        location.href = prefix + "/getSimpleReport";
        // location.href = prefix + "/index";
        // window.navigate("http://localhost:8081/battery/index");
    }

    function exportBatteryTotalData() {
        $.fileDownload(prefix + "/exportBatteryTotalData", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text(),
                count:$("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                // sort: $("#sort").val().trim(),
                orderNum: $("#orderNum").val().trim(),
                carNum: $("#carNum").val().trim(),
                carVin: $("#carVin").val().trim(),
                // bizType: $("#bizType").val().trim(),
                isError: $("#isError").val().trim(),
                carType: $("#carType").val().trim(),
                reportStartTime: $("#beginTime").val().trim(),
                reportEndTime: $("#endTime").val().trim(),
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })

    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        load: load,
        getSumData: getSumData,
        exportBatteryTotalData: exportBatteryTotalData,
        detailBattery: detailBattery
    };
})();
